import { TAllTypes, IValue, IValueNumber, IPosition, IRange, IValueRange, IAllPosition } from '../../types';
import { AFunc, IPositionValue } from '../types';
import { calculate } from '../../calculate';
import { FormulaError } from '../../formula-error';
import { isEqualVal, isEqualCondition, parseToNumber } from '../../utils';


export class SUMIF extends AFunc {
  constructor(protected positionVal: IPositionValue) {
    super(positionVal);
  }
  protected getEmptyVal(): IValueNumber {
    return {type: 'value', dataType: 'number', value: 0};
  }
  public excute(args: TAllTypes[]): TAllTypes[] {
    if(args.length !== 3 && args.length !== 2) {
      throw new FormulaError('#VALUE!', {errorId: '0082a0d002aa'});
    }
    if((args[1].type !== 'value' || ((args[1] as IValue).dataType !== 'string' && (args[1] as IValue).dataType !== 'number')) && (args[1] as IAllPosition).type !== 'position') {
      throw new FormulaError('#UNSUPPORT', {errorId: 'c9cda03a91ae'});
    }
    let range = this.transformToRangeValue(args[0]);
    let criteria = this.transformToRangeValue(args[1])[0][0];
    let sumRang = this.transformToRangeValue(args[2] ? args[2] : args[0]);
    let result: IValue = {type: 'value', dataType: 'number', value: 0};
    let arr: IValue[] = [];
    for(let i = 0; i < range.length; i++) {
      if(!Array.isArray(range[i]) || !Array.isArray(sumRang[i])) continue;
      for(let j = 0; j < range[i].length; j++) {
        if(!range[i][j] || !sumRang[i][j] || (sumRang[i][j].dataType === 'string' && isNaN(Number(sumRang[i][j].value)))) continue;
        if(isEqualCondition(range[i][j], criteria.value.toString())) {
          arr.push(sumRang[i][j]);
        }
      }
    }
    for(let item of arr) {
      result = calculate('+', [result, item]) as IValue;
    }
    return [result];
    // if(args[1].type === 'position') {
    //   let temp = this.positionVal.getValue(args[1]);
    //   if(!temp[0]) {
    //     args[1] = {type: 'value', dataType: 'string', value: ''}
    //   } else {
    //     args[1] = temp[0] as TAllTypes;
    //   }
    // }
    // if(args[1].type === 'value') {
    //   if(args[0].type === 'value' || args[0].type === 'position') {
    //     if(args[0].type === 'position') {
    //       let temp = this.positionVal.getValue(args[0]);
    //       if(!temp[0]) {
    //         args[0] = {type: 'value', dataType: 'string', value: ''}
    //       } else {
    //         args[0] = temp[0] as TAllTypes;
    //       }
    //     }
    //     if(isEqualVal((args[0] as IValue), args[1])) {
    //       if(args[3]) {
    //         if(args[3].type === 'value') {
    //           return [args[3]];
    //         }
    //         if(args[3].type === 'position' || args[3].type === 'colPosition' || args[3].type === 'rowPosition') {
    //           let _val = this.positionVal.getValue(args[3]);
    //           if(_val[0]) {
    //             return [(_val[0] as TAllTypes)];
    //           } else {
    //             return [this.getEmptyVal()];
    //           }
    //         }
    //       } else {
    //         return [this.getEmptyVal()];
    //       }
    //     }
    //   } else {
    //     let vals = (args[0] as IRange).value.getMatrixValues();
    //     if(vals.length === 0) {
    //       vals = [[]];
    //     }
    //     let sumOriginVals = this.transformToRangeValue(args[3]);
    //     let resultVal: IValueNumber = this.getEmptyVal();
    //     for(let i = 0; i < vals.length; i++) {
    //       if(isEqualVal(vals[i][0], args[1]) && sumOriginVals[i][0] && sumOriginVals[i][0].type === 'value') {
    //         resultVal = calculate('+', [resultVal, sumOriginVals[i][0]]) as IValueNumber;
    //       }
    //     }
    //     return [resultVal];
    //   }
    // } else {
    //   let criteria: IValue[][] = this.transformToRangeValue(args[1]);
    //   for(let item of criteria) {
    //     if(item.length > 1) {
    //       return [this.getEmptyVal()];
    //     }
    //   }
    //   let range: IValue[][] = this.transformToRangeValue(args[0]);
    //   let sumRang: IValue[][] = this.transformToRangeValue(args[0]);
    //   let resultVal = this.getEmptyVal();
    //   for(let i = 0; i < range.length; i++) {
    //     if(range[i].length === 0) {
    //       continue;
    //     }
    //     if(criteria[i] && criteria[i][0] && isEqualVal(range[i][0], criteria[i][0]) && sumRang[i] && sumRang[i][0]) {
    //       resultVal = calculate('+', [resultVal, sumRang[i][0]]) as IValueNumber;
    //     }
    //   }
    //   return [resultVal];
    // }

    return [];
  }
}